﻿#encoding:utf-8
import common,string

class COMMAND:
	def __init__(self,files,strindex):
		self.files=files
		self.strindex=strindex
		
	def send(self,command):
		#命令解析
		fun={}
		cmd=''
		isparam=False
		for str in command:
			if str==' ':
				isparam=True
				break
		if isparam:
			list_cmd=command.split(' ')
			cmd=list_cmd[0].replace('-','')
			parameter=list_cmd[1]
		
			#此处增加带参数命令，暂只带一个参数
			fun={
				'run':lambda:self._addstart(parameter),
				'del':lambda:self._deletestart(parameter),
			}
		else:
			cmd=command.replace('-','')
			#此处增加不带参数命令
			fun={
				'all':lambda:self._getallstart(),
				'quit':lambda:common.close(),
			}
		
		isexist=False
		for key in fun.keys():
			if cmd==key:
				isexist=True
				
		if isexist:
			return fun[cmd]()
		else:
			return ['not found "'+cmd+'" command']

	def _getallstart(self):
		#所有快速启动信息
		json=common.getJson()
		list_start=[]
		for key in json.keys():
			path=json[key]
			list_path=path.split('\\')
			path=list_path[len(list_path)-1]
			list_start.append('key:'+key+'\npath:'+path)
		return list_start

	def _addstart(self,index):
		#增加快速启动信息
		print self.files.values()
		filepath=self.files.values()[int(index)]
		json=common.getJson()
		json[self.strindex]=filepath
		common.setJson(json)
		
		common.runapp(self.files.values()[int(index)])
		
	def _deletestart(self,index):
		#删除快速启动信息
		json=common.getJson()
		path=''
		del json[index]
		common.setJson(json)
		return self._getallstart()
		